package com.yytek.am.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.yytek.common.core.domain.R;
import com.yytek.common.security.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yytek.common.log.annotation.Log;
import com.yytek.common.log.enums.BusinessType;
import com.yytek.common.security.annotation.RequiresPermissions;
import com.yytek.am.domain.FmEvent;
import com.yytek.am.service.IFmEventService;
import com.yytek.common.core.web.controller.BaseController;
import com.yytek.common.core.web.domain.AjaxResult;
import com.yytek.common.core.utils.poi.ExcelUtil;
import com.yytek.common.core.web.page.TableDataInfo;

/**
 * 活动告警事件Controller
 * 
 * @author yytek-iot
 * @date 2022-10-10
 */
@Api(value = "活动告警控制器", tags = {"活动告警管理"})
@RestController
@RequestMapping("/event")
public class FmEventController extends BaseController
{
    @Autowired
    private IFmEventService fmEventService;
    /**
     * 获取各种告警类型数量
     */
    @ApiOperation("获取各种告警类型数量")
    @GetMapping("/allTypeNum")
    public R allTypeNum(FmEvent fmEvent)
    {
        Map<String,Object> num = fmEventService.selectAllTypeNum(fmEvent);
        return R.ok(num);
    }

    /**
     * 分页查询活动告警事件列表
     */
    @ApiOperation("分页查询活动告警事件列表")
    @RequiresPermissions("am:event:list")
    @GetMapping("/list")
    public TableDataInfo list(FmEvent fmEvent)
    {
        startPage();
        List<FmEvent> list = fmEventService.selectFmEventList(fmEvent);
        return getDataTable(list);
    }

    /**
     * 导出告警事件列表
     */
    @ApiOperation("导出告警事件列表")
    @RequiresPermissions("am:event:export")
    @Log(title = "告警事件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FmEvent fmEvent)
    {
        List<FmEvent> list = fmEventService.selectFmEventList(fmEvent);
        ExcelUtil<FmEvent> util = new ExcelUtil<FmEvent>(FmEvent.class);
        util.exportExcel(response, list, "告警事件数据");
    }

    /**
     * 获取告警事件详细信息
     */
    @ApiOperation("获取告警事件详细信息")
    @RequiresPermissions("am:event:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(fmEventService.selectFmEventById(id));
    }

    /**
     * 新增告警事件
     */
    @ApiOperation("新增告警事件")
    @RequiresPermissions("am:event:add")
    @Log(title = "告警事件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FmEvent fmEvent)
    {
        return toAjax(fmEventService.insertFmEvent(fmEvent));
    }
    /**
     * 活动告警清除
     */
    @ApiOperation("活动告警清除")
    @PostMapping(value = "/clear")
    public AjaxResult clear(String id,String note)
    {
        return toAjax(fmEventService.clearEvent(id,note));
    }

    /**
     * 活动告警确认
     */
    @ApiOperation("活动告警确认")
    @RequiresPermissions("am:event:edit")
    @Log(title = "活动告警确认", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/confirm")
    public AjaxResult confirm(@RequestBody FmEvent fmEvent)
    {
        //告警确认人
        String userId = SecurityUtils.getUserId().toString();
        fmEvent.setAckTime(new Date());
        fmEvent.setAckOperator(userId);
        return toAjax(fmEventService.updateFmEvent(fmEvent));
    }

    /**
     * 修改告警事件
     */
    @ApiOperation("修改告警事件")
    @RequiresPermissions("am:event:edit")
    @Log(title = "告警事件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FmEvent fmEvent)
    {
        return toAjax(fmEventService.updateFmEvent(fmEvent));
    }

    /**
     * 删除告警事件
     */
    @ApiOperation("删除告警事件")
    @RequiresPermissions("am:event:remove")
    @Log(title = "告警事件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fmEventService.deleteFmEventByIds(ids));
    }
}
